package com.szh.zx.gaoDingJavaBook.chapter8.six;

import java.util.Hashtable;

/**
 * 测试hashtable
 */
class A {
	int count;

	public A(int count) {
		this.count = count;
	}
//

	@Override
	public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		}
		if (obj != null && obj.getClass() == A.class) {
			A a = (A) obj;
			return this.count == a.count;
		}
		return false;
	}

	@Override
	public int hashCode() {
		return this.count;
	}

	@Override
	public String toString() {
		return "A{" +
				"count=" + count +
				'}';
	}
}
class B{
	@Override
	public boolean equals(Object obj) {
		return true;
	}
}

public class HashtableTest {
	public static void main(String[] args) {
		Hashtable hashtable = new Hashtable();
		hashtable.put(new A(999),"疯狂java3");
		hashtable.put(new A(9),"疯狂java2");
		hashtable.put(new A(99),"疯狂java1");
		hashtable.put(new A(9998),new B());
		System.out.println(hashtable);
//		由于hashtable中有一个b对象，它与然和对象比对，都是true
		System.out.println(hashtable.contains("测试字符串"));
//      只有俩个A对象的count相等，他们通过euqals返回true，hashtable认为是相同的key
		System.out.println(hashtable.containsKey(new A(9998)));















	}
}
